package wtc.stu.dao;

import wtc.stu.entity.StuEntity;
import wtc.stu.util.DBConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class StuDaoDepartment {
    private DBConnection dbc;
    private Connection conn = null;
    private ResultSet rs = null;
    private PreparedStatement pre = null;
    public StuDaoDepartment()
    {
        dbc = new DBConnection();

    }
    // 全查询系别信息
    public List<StuEntity> getAll()
    {
        List<StuEntity> ar = new ArrayList<StuEntity>();
        try
        {
            conn = dbc.getConnection();
            String sql = "select * from departmentinfo";
            pre = conn.prepareStatement(sql);
            rs = pre.executeQuery();
            while (rs.next())
            {
                StuEntity department = new StuEntity();
                department.setXid(rs.getInt("xid"));
                department.setXname(rs.getString("xname"));
                department.setXremark(rs.getString("xremark"));
                ar.add(department);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
        return ar;
    }

    // 查询一个系别信息表对象
    public StuEntity getOneById(int xid)
    {
        StuEntity department = new StuEntity();

        try
        {
            conn = dbc.getConnection();
            String sql = "select * from departmentinfo where xid=?";
            pre = conn.prepareStatement(sql);
            pre.setInt(1, xid);
            rs = pre.executeQuery();
            while (rs.next())
            {
                department.setXid(rs.getInt("xid"));
                department.setXname(rs.getString("xname"));
                department.setXremark(rs.getString("xremark"));

            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
        return department;
    }

    // 增加系别信息表对象
    public void addStu(StuEntity department)
    {
        try
        {
            conn = dbc.getConnection();
            String sql = "insert into departmentinfo values(?,?,?)";
            pre = conn.prepareStatement(sql);
            pre.setInt(1, department.getXid());
            pre.setString(2,department.getXname());
            pre.setString(3, department.getXremark());
            pre.executeUpdate();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
    }

    // 修改系别信息表对象
    public void updateStu(StuEntity stu1)
    {
        try
        {
            conn = dbc.getConnection();
            String sql = "update  departmentinfo  set xname=?,xremark=? where xid=?";
            pre = conn.prepareStatement(sql);
            pre.setString(1, stu1.getXname());
            pre.setString(2, stu1.getXremark());
            pre.setInt(3, stu1.getXid());
            pre.executeUpdate();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
    }

    // 删除年级信息表对象
    public void delStu(int Xid)
    {
        try
        {
            conn = dbc.getConnection();
            String sql = "delete from departmentinfo where xid=?";
            pre = conn.prepareStatement(sql);
            pre.setInt(1,Xid);
            pre.executeUpdate();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
    }
}

